home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / chrome_100_percent.pak / Unnamed File 000008.txt < prev    next >
Text File  |  2013-04-03  |  10KB  |  261 lines

  1. // Copyright 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. var chrome;
  6. if (!chrome)
  7.   chrome = {};
  8. if (!chrome.searchBox) {
  9.   chrome.searchBox = new function() {
  10.     var safeObjects = {};
  11.     chrome.searchBoxOnWindowReady = function() {
  12.       // |searchBoxOnWindowReady| is used for initializing window context and
  13.       // should be called only once per context.
  14.       safeObjects.createShadowRoot = Element.prototype.webkitCreateShadowRoot;
  15.       safeObjects.defineProperty = Object.defineProperty;
  16.       delete window.chrome.searchBoxOnWindowReady;
  17.     };
  18.  
  19.     // =========================================================================
  20.     //                                  Constants
  21.     // =========================================================================
  22.     var MAX_CLIENT_SUGGESTIONS_TO_DEDUPE = 6;
  23.     var MAX_ALLOWED_DEDUPE_ATTEMPTS = 5;
  24.  
  25.     var HTTP_REGEX = /^https?:\/\//;
  26.  
  27.     var WWW_REGEX = /^www\./;
  28.  
  29.     // =========================================================================
  30.     //                            Private functions
  31.     // =========================================================================
  32.     native function GetQuery();
  33.     native function GetVerbatim();
  34.     native function GetSelectionStart();
  35.     native function GetSelectionEnd();
  36.     native function GetX();
  37.     native function GetY();
  38.     native function GetWidth();
  39.     native function GetHeight();
  40.     native function GetStartMargin();
  41.     native function GetEndMargin();
  42.     native function GetRightToLeft();
  43.     native function GetAutocompleteResults();
  44.     native function GetContext();
  45.     native function GetDisplayInstantResults();
  46.     native function GetFont();
  47.     native function GetFontSize();
  48.     native function GetThemeBackgroundInfo();
  49.     native function GetThemeAreaHeight();
  50.     native function IsKeyCaptureEnabled();
  51.     native function NavigateContentWindow();
  52.     native function SetSuggestions();
  53.     native function SetQuerySuggestion();
  54.     native function SetQuerySuggestionFromAutocompleteResult();
  55.     native function SetQuery();
  56.     native function SetQueryFromAutocompleteResult();
  57.     native function Show();
  58.     native function StartCapturingKeyStrokes();
  59.     native function StopCapturingKeyStrokes();
  60.  
  61.     function escapeHTML(text) {
  62.       return text.replace(/[<>&"']/g, function(match) {
  63.         switch (match) {
  64.           case '<': return '<';
  65.           case '>': return '>';
  66.           case '&': return '&';
  67.           case '"': return '"';
  68.           case "'": return ''';
  69.         }
  70.       });
  71.     }
  72.  
  73.     // Returns the |restrictedText| wrapped in a ShadowDOM.
  74.     function SafeWrap(restrictedText) {
  75.       var node = document.createElement('div');
  76.       var nodeShadow = safeObjects.createShadowRoot.apply(node);
  77.       nodeShadow.applyAuthorStyles = true;
  78.       nodeShadow.innerHTML =
  79.           '<div style="width:700px!important;' +
  80.           '            height:22px!important;' +
  81.           '            font-family:\'' + GetFont() + '\',\'Arial\'!important;' +
  82.           '            overflow:hidden!important;' +
  83.           '            text-overflow:ellipsis!important">' +
  84.           '  <nobr>' + restrictedText + '</nobr>' +
  85.           '</div>';
  86.       safeObjects.defineProperty(node, 'webkitShadowRoot', { value: null });
  87.       return node;
  88.     }
  89.  
  90.     // Wraps the AutocompleteResult query and URL into ShadowDOM nodes so that
  91.     // the JS cannot access them and deletes the raw values.
  92.     function GetAutocompleteResultsWrapper() {
  93.       var autocompleteResults = DedupeAutocompleteResults(
  94.           GetAutocompleteResults());
  95.       var userInput = GetQuery();
  96.       for (var i = 0, result; result = autocompleteResults[i]; ++i) {
  97.         var title = escapeHTML(result.contents);
  98.         var url = escapeHTML(CleanUrl(result.destination_url, userInput));
  99.         var combinedHtml = '<span class=chrome_url>' + url + '</span>';
  100.         if (title) {
  101.           result.titleNode = SafeWrap(title);
  102.           combinedHtml += '<span class=chrome_separator> – </span>' +
  103.               '<span class=chrome_title>' + title + '</span>';
  104.         }
  105.         result.urlNode = SafeWrap(url);
  106.         result.combinedNode = SafeWrap(combinedHtml);
  107.         delete result.contents;
  108.         delete result.destination_url;
  109.       }
  110.       return autocompleteResults;
  111.     }
  112.  
  113.     // TODO(dcblack): Do this in C++ instead of JS.
  114.     function CleanUrl(url, userInput) {
  115.       if (url.indexOf(userInput) == 0) {
  116.         return url;
  117.       }
  118.       url = url.replace(HTTP_REGEX, '');
  119.       if (url.indexOf(userInput) == 0) {
  120.         return url;
  121.       }
  122.       return url.replace(WWW_REGEX, '');
  123.     }
  124.  
  125.     // TODO(dcblack): Do this in C++ instead of JS.
  126.     function CanonicalizeUrl(url) {
  127.       return url.replace(HTTP_REGEX, '').replace(WWW_REGEX, '');
  128.     }
  129.  
  130.     // Removes duplicates from AutocompleteResults.
  131.     // TODO(dcblack): Do this in C++ instead of JS.
  132.     function DedupeAutocompleteResults(autocompleteResults) {
  133.       var urlToResultMap = {};
  134.       for (var i = 0, result; result = autocompleteResults[i]; ++i) {
  135.         var url = CanonicalizeUrl(result.destination_url);
  136.         if (url in urlToResultMap) {
  137.           var oldRelevance = urlToResultMap[url].rankingData.relevance;
  138.           var newRelevance = result.rankingData.relevance;
  139.           if (newRelevance > oldRelevance) {
  140.             urlToResultMap[url] = result;
  141.           }
  142.         } else {
  143.           urlToResultMap[url] = result;
  144.         }
  145.       }
  146.       var dedupedResults = [];
  147.       for (url in urlToResultMap) {
  148.         dedupedResults.push(urlToResultMap[url]);
  149.       }
  150.       return dedupedResults;
  151.     }
  152.  
  153.     var lastPrefixQueriedForDuplicates = '';
  154.     var numDedupeAttempts = 0;
  155.  
  156.     function DedupeClientSuggestions(clientSuggestions) {
  157.       var userInput = GetQuery();
  158.       if (userInput == lastPrefixQueriedForDuplicates) {
  159.         numDedupeAttempts += 1;
  160.         if (numDedupeAttempts > MAX_ALLOWED_DEDUPE_ATTEMPTS) {
  161.           // Request blocked for privacy reasons.
  162.           // TODO(dcblack): This check is insufficient.  We should have a check
  163.           // such that it's only callable once per onnativesuggestions, not
  164.           // once per prefix.  Also, there is a timing problem where if the user
  165.           // types quickly then the client will (correctly) attempt to render
  166.           // stale results, and end up calling dedupe multiple times when
  167.           // getValue shows the same prefix.  A better solution would be to have
  168.           // the client send up rid ranges to dedupe against and have the
  169.           // binary keep around all the old suggestions ever given to this
  170.           // overlay.  I suspect such an approach would clean up this code quite
  171.           // a bit.
  172.           return false;
  173.         }
  174.       } else {
  175.         lastPrefixQueriedForDuplicates = userInput;
  176.         numDedupeAttempts = 1;
  177.       }
  178.  
  179.       var autocompleteResults = GetAutocompleteResults();
  180.       var nativeUrls = {};
  181.       for (var i = 0, result; result = autocompleteResults[i]; ++i) {
  182.         var nativeUrl = CanonicalizeUrl(result.destination_url);
  183.         nativeUrls[nativeUrl] = result.rid;
  184.       }
  185.       for (var i = 0; clientSuggestions[i] &&
  186.            i < MAX_CLIENT_SUGGESTIONS_TO_DEDUPE; ++i) {
  187.         var result = clientSuggestions[i];
  188.         if (result.url) {
  189.           var clientUrl = CanonicalizeUrl(result.url);
  190.           if (clientUrl in nativeUrls) {
  191.             result.duplicateOf = nativeUrls[clientUrl];
  192.           }
  193.         }
  194.       }
  195.       return true;
  196.     }
  197.  
  198.     // =========================================================================
  199.     //                           Exported functions
  200.     // =========================================================================
  201.     this.__defineGetter__('value', GetQuery);
  202.     this.__defineGetter__('verbatim', GetVerbatim);
  203.     this.__defineGetter__('selectionStart', GetSelectionStart);
  204.     this.__defineGetter__('selectionEnd', GetSelectionEnd);
  205.     this.__defineGetter__('x', GetX);
  206.     this.__defineGetter__('y', GetY);
  207.     this.__defineGetter__('width', GetWidth);
  208.     this.__defineGetter__('height', GetHeight);
  209.     this.__defineGetter__('startMargin', GetStartMargin);
  210.     this.__defineGetter__('endMargin', GetEndMargin);
  211.     this.__defineGetter__('rtl', GetRightToLeft);
  212.     this.__defineGetter__('nativeSuggestions', GetAutocompleteResultsWrapper);
  213.     this.__defineGetter__('isKeyCaptureEnabled', IsKeyCaptureEnabled);
  214.     this.__defineGetter__('context', GetContext);
  215.     this.__defineGetter__('displayInstantResults', GetDisplayInstantResults);
  216.     this.__defineGetter__('themeBackgroundInfo', GetThemeBackgroundInfo);
  217.     this.__defineGetter__('themeAreaHeight', GetThemeAreaHeight);
  218.     this.__defineGetter__('font', GetFont);
  219.     this.__defineGetter__('fontSize', GetFontSize);
  220.     this.setSuggestions = function(text) {
  221.       SetSuggestions(text);
  222.     };
  223.     this.setAutocompleteText = function(text, behavior) {
  224.       SetQuerySuggestion(text, behavior);
  225.     };
  226.     this.setRestrictedAutocompleteText = function(resultId) {
  227.       SetQuerySuggestionFromAutocompleteResult(resultId);
  228.     };
  229.     this.setValue = function(text, type) {
  230.       SetQuery(text, type);
  231.     };
  232.     this.setRestrictedValue = function(resultId) {
  233.       SetQueryFromAutocompleteResult(resultId);
  234.     };
  235.     this.show = function(reason, height) {
  236.       Show(reason, height);
  237.     };
  238.     this.markDuplicateSuggestions = function(clientSuggestions) {
  239.       return DedupeClientSuggestions(clientSuggestions);
  240.     };
  241.     this.navigateContentWindow = function(destination) {
  242.       return NavigateContentWindow(destination);
  243.     };
  244.     this.startCapturingKeyStrokes = function() {
  245.       StartCapturingKeyStrokes();
  246.     };
  247.     this.stopCapturingKeyStrokes = function() {
  248.       StopCapturingKeyStrokes();
  249.     };
  250.     this.onchange = null;
  251.     this.onsubmit = null;
  252.     this.oncancel = null;
  253.     this.onresize = null;
  254.     this.onautocompleteresults = null;
  255.     this.onkeypress = null;
  256.     this.onkeycapturechange = null;
  257.     this.oncontextchange = null;
  258.     this.onmarginchange = null;
  259.   };
  260. }
  261.